/*****************************************************************************
*    Ali Corp. All Rights Reserved. 2002 Copyright (C)
*
*    File:    usb.h
*
*    Description:    This file contains interface functions declare
*		             of USB driver.
*    History:
*           Date            Athor        Version          Reason
*	    ============	=============	=========	=================
*	1.	Jul.10.2007      Victor chen      Ver 0.1    Create file.
*****************************************************************************/

#ifndef __USB_NEW_H__
#define __USB_NEW_H__

#include <basic_types.h>
//#include <bus/usb2/usb_dev.h>

//#define USB_INFO
//#define USB_DEBUG

//#define USB_DEBUG_ERR     //if debug ,please open this MACRO
#ifndef USB_DEBUG_PRINTF
//#define USB_DEBUG_PRINTF libc_printf
//#define USB_DEBUG_PRINTF soc_printf
#define USB_DEBUG_PRINTF(...)	do{}while(0)
#endif

#define USB_COMMAND_TIMEOUT  3
struct  usb_dev_info_ex
{
	UINT16 bcdUSB;
	UINT8  bDeviceClass;
	UINT8  bDeviceSubClass;
	UINT8  bDeviceProtocol;
	UINT16 idVendor;
	UINT16 idProduct;
	UINT16 bcdDevice;
};

struct  usb_disk_info_ex
{
	UINT16 bcdUSB;
	UINT16 idVendor;
	UINT16 idProduct;
	UINT8  iSerialNumber;
	UINT8 subclass;
	UINT8 protocol;
	UINT8 max_lun;
	UINT32 phy_lbn0[16];
	UINT32 disk_size[16];
	UINT32 blk_size[16];
	UINT32 speed;	//real speed
	UINT32 which_port;
    UINT32 host_id;
};

struct usb_debug_info
{
    struct usb_disk_info_ex disk;
	UINT32 read_retry_counter;
	UINT32 write_retry_counter;
	UINT32 stall_err;
	UINT32 rsp_err;
	UINT32 tog_err;
	UINT32 pid_err;
	UINT32 utmi_err;
	UINT32 crc_err;
	UINT32 unexpct_err;
    UINT32 usb_speed;
};

typedef void* USBD_NODE_ID, * pUSBD_NODE_ID;
#define MAX_HANDLE_NUM 32

#define USB_HOST0_EN       0x1
#define USB_HOST1_EN       0x2

/**************************************************************************************
 usbd_attach -  To attach host and mass-storage device driver.

 Parms:
    UINT8 host_en   :		host_en is set by caller to choose the port attach for M3606
    					host_en value should be USB_HOST0_EN  or  USB_HOST1_EN or USB_HOST0_EN|USB_HOST1_EN	
    void(*notify)(UINT32): A callback for the upper layer. It will inform upper layer
    					   the usb device is connected to or removed from the port.
    void(*mount)(UINT32): A callback for the upper layer to mount device.
    void(*umount)(UINT32): A callback for the upper layer to umount device.

 Returns:
    RET_SUCCESS = 0	: successful.
    !RET_SUCCESS	: failure.
 ***************************************************************************************/
RET_CODE usbd_m36f_attach(UINT32 host_en ,  void(*notify) (UINT32), void(*mount) (UINT32), void(*umount) (UINT32)  );



/**************************************************************************************
 usbd_attach -  To attach host and mass-storage device driver.

 Parms:
    void(*notify)(UINT32): A callback for the upper layer. It will inform upper layer
    					   the usb device is connected to or removed from the port.
    void(*mount)(UINT32): A callback for the upper layer to mount device.
    void(*umount)(UINT32): A callback for the upper layer to umount device.

 Returns:
    RET_SUCCESS = 0	: successful.
    !RET_SUCCESS	: failure.
 ***************************************************************************************/
RET_CODE usbd_attach_ex(void(*notify)(UINT32), void(*mount)(UINT32), void(*umount)(UINT32));


/***************************************************************************************
 usbd_device_ready - To check whether the device is ready for accessing.
 					 That is means the device already have mounted by OS,
 					 and can accessing to the device.

 Parms:
    lun:	The index number of the device.

 Returns:
    RET_SUCCESS		: Device is ready for accessing.
    !RET_SUCCESS	: Device is not ready for accessing.
 ***************************************************************************************/
RET_CODE usbd_device_ready_ex(USBD_NODE_ID NodeId,UINT32 lun);

/**************************************************************************************
 usbd_device_get_disk_count -  To get a count of the device that connected on the port.

 Parms:
	struct usb_disk_info *disk_info: pointer to point usb disk info structure.

 Returns:
     RET_SUCCESS	: successful;
    !RET_SUCCESS	: failure;
 ***************************************************************************************/
INT32  usbd_get_disk_info_ex(USBD_NODE_ID NodeId,struct usb_disk_info_ex *disk_info);

/**************************************************************************************
 usbd_device_get_disk_count -  To get a count of the device that connected on the port.

 Parms:
	none:

 Returns:
    unsigned long value.
 ***************************************************************************************/
UINT32 usbd_device_get_disk_count_ex(USBD_NODE_ID NodeId);


/**************************************************************************************
 usbd_device_get_disk_image -  To return a bitmap of mounted device
							(the index number of device is pointed by position of bitmap).
 Parms:
	none:

 Returns:
    unsigned long value.
 ***************************************************************************************/
UINT32 usbd_device_get_disk_image_ex(USBD_NODE_ID NodeId);


/***************************************************************************************
 usbd_get_disk_size - To get the disk size of the usb device.

 Parms:
    lun:	The index number of the device.

 Returns:
    	0:				The usb device not work or not exist.
	other value:		size of disk.
 ***************************************************************************************/
INT32 usbd_get_disk_size_ex(USBD_NODE_ID NodeId,UINT8 lun);
INT64 usbd_get_disk_size_ex_16(USBD_NODE_ID NodeId,UINT8 lun);

/**************************************************************************************
 usbd_device_read -  To read data form usb mass-storage device.

 Parms:
	sub_lun:			Index number of device.
	lbn:				local block number (Sector number), point to the beginning.
	buffer:				A pointer to point a buffer where will save the data that read from
						usb mass-storage device.
	sector_count:		Total sector count that will be read.

 Returns:
    RET_SUCCESS = 0	: successful.
    !RET_SUCCESS	: failure.
 ***************************************************************************************/
INT32 usbd_device_read_ex(USBD_NODE_ID NodeId,UINT8 sub_lun, UINT32 lbn, UINT8* buffer, UINT32 sector_count);
INT32 usbd_device_read_ex_16(USBD_NODE_ID NodeId,UINT8 sub_lun, UINT64 lbn, UINT8* buffer, UINT32 sector_count);

/**************************************************************************************
 usbd_device_write -  To write data to usb mass-storage device.

 Parms:
	sub_lun:			Index number of device.
	lbn:				local block number (Sector number), point to the beginning.
	buffer:				A pointer to point a buffer where store the source data that will be
						write to usb mass-storage device.
	sector_count:		Total sector count that will be write.

 Returns:
    RET_SUCCESS = 0	: successful.
    !RET_SUCCESS	: failure.
 ***************************************************************************************/
INT32 usbd_device_write_ex(USBD_NODE_ID NodeId, UINT8 sub_lun, UINT32 lbn, UINT8* buffer, UINT32 sector_count);
INT32 usbd_device_write_ex_16(USBD_NODE_ID NodeId, UINT8 sub_lun, UINT64 lbn, UINT8* buffer, UINT32 sector_count);

void usb_inhibit_rw(int inhibit);
void force_usb_suspend(void);
BOOL usb_rw_completed(void);

/****************************************
* struct IfEvtMsg_t  is msg to usb_logo_test  from UI/usbd driver
*
******************************************/

typedef void * pDEVICE_DESCRIPTOR_T  ; //point to struct usb_device_descriptor


typedef enum
{
	IF_NO_EVENT_ASSIGN = 0,		/* No Event to be Notified */
	IF_UI_EVENT_TYPE,			/* UI Event Type to be Notified */
	IF_USBD_EVENT_TYPE,			/* USBD Event Type to be Notified */
} IfEventType;


typedef enum
{
	IF_NO_SUB_EVENT_ASSIGN = 0,		/* No Event to be Notified */
	IF_UI_ENTER_SUB_TYPE,			/* UI Event Type to be Notified */
	IF_USBD_SHOW_TPL_TYPE ,
} IfEventSubType;

typedef enum
{
	IF_SUB_EVENT_TYPE ,
}IfEventVid ;

#define USB_OPT_VID 		 0x1a0a

typedef enum
{
	 PID_test_se0 		=	0x0101,
	 PID_test_J			=	 0x0102,
 	 PID_test_K 			=	0x0103,
	 PID_test_packet		=	0x0104,
	 PID_test_suspend 	=	0x0106,
	 PID_test_get_desc 	=	0x0107,
	 PID_test_get_desc_data = 0x0108,
 	 PID_test_bus_enumeration  = 0x0109,
}IfEventPid ;


#define IfEventPort_id 			UINT32 
#define IF_REAR_PROT				1 		//usb port default for M3601E/M3601S
#define IF_FRONT_PROT			0

typedef enum
{
	USBD_MASS_DEV			,
	USBD_HUB_DEV			,
	USBD_NOT_SUPPORT_DEV		,
	USBD_UNKNOWN_DEV		,
}IfEventDev_Type; 

typedef struct
{
	IfEventType				event_type;
	IfEventSubType				sub_type ;
	IfEventVid					vid;
	IfEventPid					pid ;
	IfEventPort_id				port_id ;
	IfEventDev_Type			dev_type ;	
	pDEVICE_DESCRIPTOR_T		dev_desc_pt	;
}IfEvtMsg_t ;



enum if_test_flag
{
	IF_TEST_NULL = 0,
	IF_TEST_SE0,
	IF_TEST_J,
	IF_TEST_K,
	IF_TEST_PACKET,
	IF_TEST_SUSPEND,
	IF_TEST_GET_DESC,
	IF_TEST_GET_DESC_DATA,
	IF_TEST_STOP,
	IF_TEST_ENTER,
};



/**************************************************************************************
 usb_test_enter -  dynamic remove usb device and disable usbd int 
 			      UI should call this function when enter usb_logo_test show 
  Returns:
    RET_SUCCESS = 0	: successful.
    !RET_SUCCESS		: failure.
 ***************************************************************************************/
RET_CODE usb_test_enter(void);	 // for UI

/**************************************************************************************
 usb_test_set_port -  select test port by UI
 			      UI should call this function before any test action
  Returns:
    RET_SUCCESS = 0	: successful.
    !RET_SUCCESS		: failure.
 ***************************************************************************************/
RET_CODE usb_test_set_port(IfEventPort_id port_id) ;


/**************************************************************************************
 usb_test_start -  Send UI msg to usb_test_task  , call by UI

 Parms:
	vid:				verdor_pid
	pid				event_pid	as IfEventPid		
 Returns:
    RET_SUCCESS = 0	: successful.
    !RET_SUCCESS		: failure.
 ***************************************************************************************/
RET_CODE usb_test_start(UINT32 vid, UINT32 pid);  // for UI

/**************************************************************************************
 usb_test_stop -  stop current sub-projetc test   , call by UI

 Returns:
   RET_SUCCESS = 0 : successful.
   !RET_SUCCESS 	   : failure.

 ***************************************************************************************/
RET_CODE usb_test_stop(void);


/**************************************************************************************
 usb_test_exit -  return whole usb_logo_test   , call by UI

 Returns:
   RET_SUCCESS = 0 : successful.
   !RET_SUCCESS 	   : failure.

 ***************************************************************************************/
RET_CODE usb_test_exit(void);


/**************************************************************************************
 usb_test_start -  Send UI enum msg to usb_test_task  , call by UI.

 Parms:
	 dev_descriptor		void point to usb_device_desctiptor.
 Returns:
    RET_SUCCESS = 0	: successful.
    !RET_SUCCESS		: failure.
 ***************************************************************************************/
RET_CODE usb_test_bus_enum( pDEVICE_DESCRIPTOR_T dev_descriptor);




/**************************************************************************************
reg callback  for usb.c 
 ************************************************************************************/
void usb_test_reg_usbd_call_back( RET_CODE (* )(IfEvtMsg_t))  ;

/**************************************************************************************
 usb_test_bus_notify_ex -  USBD send msg to usb_logo_test 
					  
 Parms:
	msg:				msg from usbd			
 Returns:
    RET_SUCCESS = 0	: successful.
    !RET_SUCCESS	: failure.
 ***************************************************************************************/
RET_CODE usb_test_bus_notify_ex(IfEvtMsg_t) ;  // call by USBD

/**************************************************************************************
 usb_test_bus_notify -   empty API for usbd while do not do usb_logo_test

 Parms:
	msg:				msg from usbd		
 Returns:
    RET_SUCCESS = 0	: successful.
    !RET_SUCCESS	: failure.
 ***************************************************************************************/
RET_CODE usb_test_bus_notify(IfEvtMsg_t);  // do nothing

/**************************************************************************************
reg callback for usb_logo_test.c
 ************************************************************************************/
void usb_test_reg_ui_call_back(void(* )(UINT16 *), void(*  )(char *, char *))  ;
void usb_test_show_string(UINT16* len_display_str);
void usb_test_show_tpl(UINT16 VID,UINT16 PID) ;



/**************************************************************************************
 usb_test_init -     API for usb_logo_task init
 Parms:
	void:				
 Returns:
    RET_SUCCESS = 0	: successful.
    !RET_SUCCESS	: failure.
 ***************************************************************************************/
RET_CODE usb_test_init(void) ;
RET_CODE usbd_detach_ex();


#endif/*__USB_H__*/

